package com.huika.dealontop.control.myinfo.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.google.gson.reflect.TypeToken;
import com.huika.dealontop.GlobalApp;
import com.huika.dealontop.R;
import com.huika.dealontop.config.Constants;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;
import com.huika.dealontop.support.domain.UserModel;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.views.TitleBarHelper;


/**
 * Description:评价页面
 * Created by zhouweiyong on 2015/11/13.
 */
public class EvaluateActivity extends BaseAct {
    private LinearLayout layout_content;
    private RatingBar rb_price_ae, rb_environment_ae, rb_serve_ae;
    private TextView tv_price_ae, tv_environment_ae, tv_serve_ae;
    private EditText et_evaluate_ae;
    private TextView tv_evaluate_ae;
    private Button btn_confirm_ae;
    private float price, environment, serve;
    private String merchantID, productID, orderID, skuID;

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            merchantID = intent.getStringExtra(Constants.MERCHANTID);
            productID = intent.getStringExtra(Constants.PRODUCTID);
            orderID = intent.getStringExtra(Constants.PAY_ORDERID);
            skuID = intent.getStringExtra(Constants.SKUID);
        }
    }

    @Override
    public void setRootView() {
        setContentView(R.layout.evaluate_act);
    }

    @Override
    protected void initWidget() {
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.title_evaluate);
        titleBarHelper.setLeftMsg("");
        layout_content = (LinearLayout) findViewById(R.id.layout_content);
        rb_price_ae = (RatingBar) findViewById(R.id.rb_price_ae);
        rb_environment_ae = (RatingBar) findViewById(R.id.rb_environment_ae);
        rb_serve_ae = (RatingBar) findViewById(R.id.rb_serve_ae);
        tv_price_ae = (TextView) findViewById(R.id.tv_price_ae);
        tv_environment_ae = (TextView) findViewById(R.id.tv_environment_ae);
        tv_serve_ae = (TextView) findViewById(R.id.tv_serve_ae);
        tv_evaluate_ae = (TextView) findViewById(R.id.tv_evaluate_ae);
        et_evaluate_ae = (EditText) findViewById(R.id.et_evaluate_ae);
        btn_confirm_ae = (Button) findViewById(R.id.btn_confirm_ae);
        et_evaluate_ae.addTextChangedListener(evaluateEtWacher);
        rb_price_ae.setOnRatingBarChangeListener(ratingBarChangeListener);
        rb_environment_ae.setOnRatingBarChangeListener(ratingBarChangeListener);
        rb_serve_ae.setOnRatingBarChangeListener(ratingBarChangeListener);
        btn_confirm_ae.setOnClickListener(this);
    }

    private RatingBar.OnRatingBarChangeListener ratingBarChangeListener = new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
            switch (ratingBar.getId()) {
                case R.id.rb_price_ae:
                    tv_price_ae.setText(String.valueOf(v));
                    break;
                case R.id.rb_environment_ae:
                    tv_environment_ae.setText(String.valueOf(v));
                    break;
                case R.id.rb_serve_ae:
                    tv_serve_ae.setText(String.valueOf(v));
                    break;
            }
        }
    };

    private TextWatcher evaluateEtWacher = new TextWatcher() {
        int tvSum = 400;

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            int len = editable.toString().length();
            tv_evaluate_ae.setText(String.valueOf(400 - len));
        }
    };

    @Override
    public void widgetClick(View v) {
        evaluateNet();
    }

    private void evaluateNet() {
        if (TextUtils.isEmpty(getInputStr(et_evaluate_ae))){
            showToastMsg(R.string.evaluate_toast_no_content);
            return;
        }
        showLoadingDialog("");
        UserModel userModel = GlobalApp.userModel;
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", userModel.getUserID());
        ajaxParams.putCommonTypeParam("merchantID", merchantID);
        ajaxParams.putCommonTypeParam("productID", productID);
        ajaxParams.putCommonTypeParam("orderID", orderID);
        ajaxParams.putCommonTypeParam("skuID", skuID);
        ajaxParams.putCommonTypeParam("environment", (int) rb_environment_ae.getRating());
        ajaxParams.putCommonTypeParam("services", (int) rb_serve_ae.getRating());
        ajaxParams.putCommonTypeParam("cost", (int) rb_price_ae.getRating());
        ajaxParams.putCommonTypeParam("evaluateContent", et_evaluate_ae.getText());
        FormResultRequest<String> request = new FormResultRequest<String>(UrlConstants.SHOPEVALUATE, new Response.Listener<RequestResult<String>>() {
            @Override
            public void onResponse(RequestResult<String> arg0) {
                dismissLoadingDialog();
                showToastMsg(arg0.msg);
                EvaluateActivity.this.finish();
            }
        }, this, new TypeToken<RequestResult<String>>() {
        }.getType());
        request.setRequestParams(ajaxParams);
        executeRequest(request);
    }
}
